/* Main page of the app */
Page({
  data:{
    button_onclick:'sign_in',
    nowDate:'',
    sign_in:"签到",
    sign_in_color:"rgb(255, 182, 211)",
    home_img1_url:"../../images/Home/home-1.jpg",
    home_img2_url:"../../images/Home/home-4.jpg",
    home_img3_url:"../../images/Home/2.jpg",
    home_img4_url:"../../images/Home/3.jpg"
  },

  onShow(){
    var nowDate = this.formatDateTime();
    var towDaysBetween = this.GetNumberOfDays('2019-09-20',nowDate);
    this.setData({
      nowDate:nowDate,
      towDaysBetween:towDaysBetween
    })

    this.select_signIN()
  },

  async select_signIN(){
    const db = await getApp().database()

    getApp().getOpenId().then(async openid => {
     console.log(openid)
     console.log(this.data.nowDate)
      db.collection(getApp().globalData.collectionsignIN).where({
        _openid: openid,
        date:this.data.nowDate
      }).get().then(dataGot => {
        const {data} = dataGot;
        console.log(data.length);

        if(data.length > 0)
      {
        console.log('今天已签到')
        this.setData({
          sign_in:"已签到",
          sign_in_color:"#c3c9c7",
          button_onclick:''
        })
      }
      })

    })
  
  },

 

  formatDateTime() {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h=h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    return y+'-'+m + '-' + d;
  },

  GetNumberOfDays(date1,date2){//获得天数
    //date1：开始日期，date2结束日期
    var a1 = Date.parse(new Date(date1));
    var a2 = Date.parse(new Date(date2));
    var day = parseInt((a2-a1)/ (1000 * 60 * 60 * 24));//核心：时间戳相减，然后除以天数
    return day
  },

 
  async sign_in(){
      const db = await getApp().database()
      const _ = db.command
      console.log(this.data.nowDate)
      getApp().getOpenId().then(async openid => {
        // 增加用户和签到日期
        db.collection(getApp().globalData.collectionsignIN).add({
          data: {
           date:this.data.nowDate,
          }
        })

        // Add Credit
        db.collection(getApp().globalData.collectionUserList).where({
          _openid: openid
        }).update({
          data: {
            credit: _.inc(50)
          }
        })
      })
  
      wx.showToast({
        title: '签到成功！获得50积分。❤',
        icon: 'none',//当icon：'none'时，没有图标 只有文字
        duration: 3000
      })
      this.setData({
        sign_in:"已签到",
        sign_in_color:"#c3c9c7",
        button_onclick:''
      })
  },

  home_img1()
  {
    if(this.data.home_img1_url == "../../images/Home/1-2.jpg")
    {
      this.setData({
        home_img1_url:"../../images/Home/home-1.jpg"
      })
    }else{
      this.setData({
        home_img1_url:"../../images/Home/1-2.jpg"
      })
    }
  },
  home_img2()
  {
    if(this.data.home_img2_url == "../../images/Home/2-2.jpg")
    {
      this.setData({
        home_img2_url:"../../images/Home/home-4.jpg"
      })
    }else{
      this.setData({
        home_img2_url:"../../images/Home/2-2.jpg"
      })
    }
  },
  home_img3()
  {
    if(this.data.home_img3_url == "../../images/Home/2-3.jpg")
    {
      this.setData({
        home_img3_url:"../../images/Home/2.jpg"
      })
    }else{
      this.setData({
        home_img3_url:"../../images/Home/2-3.jpg"
      })
    }
  },
  home_img4()
  {
    if(this.data.home_img4_url == "../../images/Home/2-4.jpg")
    {
      this.setData({
        home_img4_url:"../../images/Home/3.jpg"
      })
    }else{
      this.setData({
        home_img4_url:"../../images/Home/2-4.jpg"
      })
    }
  }
})

